﻿@{
     
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<script>
    var bootstrap = function($, srAdmin) {

        var page = {
            init: function () {
                
                this.load();
                this.bind();
               

            },
            load: function () {

                top.srAdmin.clientdata.init(function () {

                    var modulesTree = srAdmin.clientdata.get(['modulesTree']);
                    // 第一级菜单
                    var parentId = '0';
                    var modules = modulesTree[parentId] || [];
                    
                    
                    var rootUI = $(' <ul class="layui-nav layui-nav-tree" lay-shrink="all" id="LAY-system-side-menu" lay-filter="layadmin-system-side-menu"></ul>');
                    for (var i = 0, l = modules.length; i < l; i++) {
                        var item = modules[i];
                        if (item.F_IsMenu == 1) {
                            
                            var $firstMenuItem = $('<li  data-name="' + item.F_ModuleId + '"  class="layui-nav-item " ></li>');
                            //if (!!item.F_Description) {
                            //    $firstMenuItem.attr('title', item.F_Description);
                            //}
                            var lia = $('<a href="javascript:;" lay-tips="' + item.F_FullName + '"  lay-direction="2" ></a>'); 
                            lia.append('<i class="layui-icon layui-icon-home"></i>');
                            lia.append('<cite>' + item.F_FullName +'</cite>');

                            //第二级菜单
                            var secondModules = modulesTree[item.F_ModuleId] || [];
                            var $secondMenus = $('<dl class="layui-nav-child"></dl>');
                            var secondMenuHad = false;
                            for (var j = 0, sl = secondModules.length; j < sl; j++) {
                                var secondItem = secondModules[j];
                                if (secondItem.F_IsMenu == 1) {
                                    secondMenuHad = true;
                                    var href = "javascript:;";
                                    if (secondItem.F_UrlAddress != null) {
                                        href = top.$.rootUrl + secondItem.F_UrlAddress;
                                    }
                                    var second_dd = $('<dd data-name="' + item.F_ModuleId + '"   ></dd>'); 
                                    var secondd_a = $('<a lay-href="' + href + '">' + secondItem.F_FullName + '</a>');
                                    second_dd.append(secondd_a);
                                    $secondMenus.append(second_dd);   

                                    // 第三级菜单
                                    var threeModules = modulesTree[secondItem.F_ModuleId] || [];
                                    var $threeMenus = $('<dl class="layui-nav-child"></dl>');
                                    var threeMenuHad = false;
                                    for (var m = 0, tl = threeModules.length ; m < tl; m++) {
                                        var threeItem = threeModules[m];
                                        if (threeItem.F_IsMenu == 1) {
                                            threeMenuHad = true;
                                            var three_dl_dd =
                                                $('<dd data-name="' + threeItem.F_ModuleId + '"><a lay-href="' + threeItem.F_UrlAddress + '">' + threeItem.F_FullName + '</a></dd>');
                                            $threeMenus.append(three_dl_dd);
                                        }
                                    }
                                    if (threeMenuHad) {
                                        second_dd.append($threeMenus);
                                    }

                                }
                            }
                            $firstMenuItem.append(lia);
                            if (secondMenuHad) {
                                $firstMenuItem.append($secondMenus);
                            } 
                           
                            rootUI.append($firstMenuItem);
                           
                        }
                       
                    }

                   
                    $('#mymenu').html(rootUI);
                    page.userInit();
                    layui.use('element', function() {
                        var element = layui.element;
                        element.init();
                    });
                    
                });


              

            },
            bind: function () {

                layui.config({
                    base: top.$.rootUrl + '/content/layui/' //静态资源所在路径
                }).extend({
                    index: 'lib/index' //主入口模块
                }).use('index');
                  
               
                    

            },
            userInit: function () {
                var loginInfo = srAdmin.clientdata.get(['userinfo']);
                //$("#userinfo").append('<img height="20" width="20" id="userhead"src="' + top.$.rootUrl + '/OrganizationModule/User/GetImg?userId=' + loginInfo.userId + '" >');
               
                $("#userinfo").append(' <cite>' + loginInfo.realName + '</cite>');
               
                var myusercenter = $("#myusercenter");
                myusercenter.append(' <dd><a lay-href="/UserCenter/Index" style="margin: 10px 5px 10px 5px"><i class="fa fa-user"></i>&nbsp;个人中心</a></dd>');
           
                if (loginInfo.isSystem) {
                    myusercenter.append(' <dd><a  id="mk_clearredis_btn" style="margin: 10px 5px 10px 5px"><i class="fa fa-refresh"></i>&nbsp;清空缓存</a></dd>');
                }
                myusercenter.append("<hr/>");
                myusercenter.append(' <dd><a  id="mk_loginout_btn" style="margin: 10px 5px 10px 5px"><i class="fa fa-power-off"></i>&nbsp;安全退出</a></dd>');
                $('#mk_loginout_btn').on('click', page.loginOut);
                $('#mk_userinfo_btn').on('click', page.openUserCenter);
                $('#mk_clearredis_btn').on('click', page.clearRedis);
            },
            loginOut: function () { // 安全退出
                srAdmin.layerConfirm("注：您确定要安全退出本次登录吗？", function (r) {
                    if (r) {
                        srAdmin.loading(true, '退出系统中...');
                        srAdmin.httpAsyncPost($.rootUrl + '/Login/OutLogin', {}, function (data) {
                            window.location.href = $.rootUrl + "/Login/Index";
                        });
                    }
                });
            },
            clearRedis: function () {//清空缓存
                srAdmin.layerConfirm("注：您确定要清空全部后台缓存数据吗？", function (r) {
                    if (r) {
                        srAdmin.loading(true, '清理缓存数据中...');
                        srAdmin.httpAsyncPost($.rootUrl + '/Home/ClearRedis', {}, function (data) {
                            window.location.href = $.rootUrl + "/Login/Index";
                        });
                    }
                });
            },
            openUserCenter: function() {//打开用户中心

            }

        };
        page.init();
    }

 


</script>